package com.ojoin.trade.supplyloan_core.web.controller;

import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.CacheUtils;
import com.ojoin.trade.common.web.controller.SimpleBaseController;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.supplyloan_core.utils.GetClassByName;
import com.ojoin.trade.supplyloan_core.utils.catchdata.MyCatchDataThreadNoMonitor;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.service.TbSlPlatformInfoService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlPlatformUserService;
import com.ojoin.trade.supplyloan_core.web.service.catchdata.ISLDataCatchBaseService;
import com.ojoin.trade.supplyloan_core.web.task.SLcatchDataTask;

@Controller
@RequestMapping("/operate/catchData")
public class SlCatchDataController extends SimpleBaseController {

	private org.slf4j.Logger log = LoggerFactory
			.getLogger(SlCatchDataController.class);

	@Autowired
	private TbSlPlatformUserService platformUserService;

	@Autowired
	private SLcatchDataTask sLcatchDataTask;

	@Autowired
	private TbSlPlatformInfoService tbSlPlatformInfoService;

	// @Async
	@ResponseBody
	@RequestMapping(value = "/runPlatformUser", method = { RequestMethod.GET,
			RequestMethod.POST })
	public Result runPlatformUser(String platformName, String platformUserId) {
		if (StringUtils.isEmpty(platformName)) {
			return new Result(Result.Status.ERROR, "请指定电商平台");
		}

		if (StringUtils.isEmpty(platformUserId)
				&& NumberUtils.isNumber(platformUserId)) {
			return new Result(Result.Status.ERROR, "请指定平台用户ID");
		}

		TbSlPlatformUser pfUser = platformUserService.queryById(Integer
				.parseInt(platformUserId));
		if (pfUser == null) {
			return new Result(Result.Status.ERROR, String.format("找不到用户%s",
					platformUserId));
		}

		try {

			sLcatchDataTask.startCatchData(pfUser);
			
			return new Result(Result.Status.OK, "抓取数据请求已发送,等待抓取");
			
		} catch (BusinessException e) {
			
			log.error(
					"platformName->" + platformName + ", 用户->"
							+ pfUser.getUsername(), e);
			
			return new Result(Result.Status.ERROR, e.getMessage());
		} 
		
	}

	@ResponseBody
	@RequestMapping(value = "/removePlatformUserId", method = RequestMethod.GET)
	public Result removePlatformUserId(String platformUserId) {
		try {
			if (!StringUtils.isEmpty(platformUserId)) {
				int uid = Integer.valueOf(platformUserId);
				sLcatchDataTask.removePlatformUserId(uid);
			}
			return new Result(Result.Status.OK, "服务运行完成");
		} catch (Exception e) {
			return new Result(Result.Status.OK, "服务运行出错");
		}

	}

	@ResponseBody
	@RequestMapping(value = "/removeAll", method = RequestMethod.GET)
	public Result removeAll() {
		try {

			sLcatchDataTask.removeAll();

			return new Result(Result.Status.OK, "服务运行完成");
		} catch (Exception e) {
			return new Result(Result.Status.OK, "服务运行出错");
		}

	}

	/*@ResponseBody
	@RequestMapping(value = "/catchdatatask", method = RequestMethod.GET)
	public Result catchdatatask() {
		try {
			//Collection<TbSlPlatformUser> c = MyCatchDataThreadNoMonitor.map.values();
			return new Result(Result.Status.OK, c);
		} catch (Exception e) {
			return new Result(Result.Status.OK, "服务运行出错");
		}

	}*/
	
	@ResponseBody
	@RequestMapping(value = "/runPlatform", method = RequestMethod.GET)
	public Result runPlatform(String platformName) {
		if (StringUtils.isEmpty(platformName)) {
			return new Result(Result.Status.ERROR, "请指定电商平台");
		}
		List<TbSlPlatformUser> userList = platformUserService
				.queryTbSlPlatformUser(platformName);

		try {
			sLcatchDataTask.startCatchData(userList);
		} catch (Exception e) {
			log.error("platformName {" + platformName + "}", e);
		} finally {
		}

		return new Result(Result.Status.OK, "服务运行完成");
	}

	@ResponseBody
	@RequestMapping(value = "/runAll", method = RequestMethod.GET)
	public Result runAll() {
		sLcatchDataTask.autoExecDataCatch();

		return new Result(Result.Status.OK, "服务运行完成");
	}

	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public Result addPlatformUser(String platformName, String username,
			String password) {
		TbSlPlatformUser user =new TbSlPlatformUser();
		user.buildDefaultEntity();
		user.setPlatformName(platformName);
		user.setIdOfTbMerchantUser(0);
		user.setUsername(username);
		user.setPassword(password);
		user.encPassword();
		platformUserService.insertTbSlPlatformUser(user);

		return new Result(Result.Status.OK, "新增平台用户完成");
	}

}
